package com.jiazhong.mingxing.boot.boot11.普通消息.消息生产;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

@Slf4j
public class 同步消息生产 {

    @SneakyThrows
    public static void main(String[] args) {
        // 1. 创建消息生产者 producer，并指定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("jiazhong-a");
        // 2. 指定 NameSrv 地址
        producer.setNamesrvAddr("124.220.170.254:9876");
        // 3. 启动 producer
        producer.start();
        for (int i = 1; i < 21; i++) {
            // 4. 创建消息对象，指定 Topic、Tag 和消息体
            String topic = "TopicA";
            String tags = "sync";
            String body = "Hello,RocketMQ!This is Sync Message,index is " + i;
            Message message = new Message(topic, tags, body.getBytes(StandardCharsets.UTF_8));
            // 5. 发送消息
            SendResult sendResult = producer.send(message);
            log.info("sendResult:{}", sendResult);
        }
        // 6. 关闭生产者 producer
        producer.shutdown();
    }
}
